﻿namespace SERFLITEMAX20
{
    using System;
    using System.Runtime.CompilerServices;
    using System.Xml;

    public static class Extentions
    {
        public static enNodeType GetAttribute(this XmlElement pElement, string pName, enNodeType pDefault)
        {
            if (pElement.HasAttribute(pName))
            {
                return pElement.GetAttribute(pName).ToNodeTypeEnum();
            }
            return pDefault;
        }

        public static enObjectTypes GetAttribute(this XmlElement pElement, string pName, enObjectTypes pDefault)
        {
            if (pElement.HasAttribute(pName))
            {
                return pElement.GetAttribute(pName).ToObjectTypeEnum();
            }
            return pDefault;
        }

        public static GameobjectTypes GetAttribute(this XmlElement pElement, string pName, GameobjectTypes pDefault)
        {
            if (pElement.HasAttribute(pName))
            {
                return pElement.GetAttribute(pName).ToGameObjectTypeEnum();
            }
            return pDefault;
        }

        public static bool GetAttribute(this XmlElement pElement, string pName, bool pDefault)
        {
            if (pElement.HasAttribute(pName))
            {
                return pElement.GetAttribute(pName).ToBool();
            }
            return pDefault;
        }

        public static int GetAttribute(this XmlElement pElement, string pName, int pDefault)
        {
            if (pElement.HasAttribute(pName))
            {
                return pElement.GetAttribute(pName).ToInt();
            }
            return pDefault;
        }

        public static long GetAttribute(this XmlElement pElement, string pName, long pDefault)
        {
            if (pElement.HasAttribute(pName))
            {
                return pElement.GetAttribute(pName).ToLong();
            }
            return pDefault;
        }

        public static float GetAttribute(this XmlElement pElement, string pName, float pDefault)
        {
            if (pElement.HasAttribute(pName))
            {
                return pElement.GetAttribute(pName).ToFloat();
            }
            return pDefault;
        }

        public static string GetAttribute(this XmlElement pElement, string pName, string pDefault)
        {
            if (pElement.HasAttribute(pName))
            {
                return pElement.GetAttribute(pName);
            }
            return pDefault;
        }

        public static string GetString(this object pValue)
        {
            if (pValue == null)
            {
                return "";
            }
            return pValue.ToString();
        }

        public static bool IsEmpty(this object pValue)
        {
            return ((((pValue == null) || pValue.Equals(string.Empty)) || DBNull.Value.Equals(pValue)) || (pValue.ToString().Trim().Length == 0));
        }

        public static bool IsEmpty(this string pValue)
        {
            return ((((pValue == null) || pValue.Equals(string.Empty)) || DBNull.Value.Equals(pValue)) || (pValue.ToString().Trim().Length == 0));
        }

        public static bool ToBool(this string pValue)
        {
            return pValue.ToBoolOrDefault(false);
        }

        public static bool ToBoolOrDefault(this string pValue, bool pDefault)
        {
            bool result = false;
            if (bool.TryParse(pValue, out result))
            {
                return result;
            }
            return pDefault;
        }

        public static float ToFloat(this string pValue)
        {
            return pValue.ToFloatOrDefault(0f);
        }

        public static float ToFloatOrDefault(this string pValue, float pDefault)
        {
            float result = 0f;
            if (float.TryParse(pValue, out result))
            {
                return result;
            }
            return pDefault;
        }

        public static GameobjectTypes ToGameObjectTypeEnum(this string pValue)
        {
            return (GameobjectTypes) pValue.ToInt();
        }

        public static int ToInt(this string pValue)
        {
            return pValue.ToIntOrDefault(0);
        }

        public static int ToIntOrDefault(this string pValue, int pDefault)
        {
            int result = 0;
            if (int.TryParse(pValue, out result))
            {
                return result;
            }
            return pDefault;
        }

        public static long ToLong(this string pValue)
        {
            return pValue.ToLongOrDefault(0L);
        }

        public static long ToLongOrDefault(this string pValue, long pDefault)
        {
            long result = 0L;
            if (long.TryParse(pValue, out result))
            {
                return result;
            }
            return pDefault;
        }

        public static enNodeType ToNodeTypeEnum(this string pValue)
        {
            return (enNodeType) pValue.ToInt();
        }

        public static enObjectTypes ToObjectTypeEnum(this string pValue)
        {
            return (enObjectTypes) pValue.ToInt();
        }
    }
}

